package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.CashHistoryListAdapter;
import com.iexbuy.ihg.adapter.CashTiXianHistoryListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.CashHistoryListObject;
import com.iexbuy.ihg.entity.CashHistoryListObjectResponce;
import com.iexbuy.ihg.entity.CashTiXianListObject;
import com.iexbuy.ihg.entity.CashTiXianListObjectResponce;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeCashWithdrawRecordsFragment extends BaseFragment {

	private static WeDeCashWithdrawRecordsFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listview_withdraw)
	private ListView listview_withdraw;
	@ViewInject(R.id.cashBalance)
	private TextView cashBalance;
	@ViewInject(R.id.tvEmpty_withdraw)
	private TextView tvEmpty_withdraw;

	private List<CashTiXianListObject> cashTiXianListObjects = new ArrayList<CashTiXianListObject>();
	private int pageNo = 0;
	private CashTiXianHistoryListAdapter cashTiXianHistoryListAdapter;

	private static String cashBalanceAmount;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_cash_withdraw_records_header, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_cashs_withdraw_records));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		cashBalance.setText(App.sPreferences.getCashBalance() + "元");
		initListData();
	}


	public void initListData() {

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", App.sPreferences.getUserId());
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_CASH_TIXIANG_HISTORY, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						CashTiXianListObjectResponce rep = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), CashTiXianListObjectResponce.class);
						if (pageNo == 0) {
							cashTiXianListObjects.clear();
						}
						cashTiXianListObjects.addAll(rep.getData());
						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {

					}
				});
	}

	private void initListView() {
		if(!(cashTiXianListObjects.size()>0)){
			tvEmpty_withdraw.setVisibility(View.VISIBLE);
		}else{
			tvEmpty_withdraw.setVisibility(View.GONE);
		}
		if (cashTiXianHistoryListAdapter == null) {
			cashTiXianHistoryListAdapter = new CashTiXianHistoryListAdapter(mBaseActivity, cashTiXianListObjects);
			listview_withdraw.setAdapter(cashTiXianHistoryListAdapter);
			listview_withdraw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					WeDeCashWithdrawRecordDetailFragment.setCashTiXianListObject(cashTiXianListObjects.get(i));
					CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCashWithdrawRecordDetailFragment.class);
				}
			});
		}
		cashTiXianHistoryListAdapter.notifyDataSetChanged();
	}

	@OnClick(R.id.withdrewCashBtn)
	public void withdrewCashBtnClick(View view) {
		if (new BigDecimal(App.sPreferences.getCashBalance()).compareTo(BigDecimal.ZERO) <= 0) {
			ToastUtils.toast(mBaseActivity, "剩余资金为0");
			return;
		}
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCashWithdrawFragment.class);
	}

	public static void setCashBalanceAmount(String cashBalanceAmount) {
		WeDeCashWithdrawRecordsFragment.cashBalanceAmount = cashBalanceAmount;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeCashWithdrawRecordsFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
